package cn.zhagzheguo.xk.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.zhagzheguo.xk.config.Constant;
import cn.zhagzheguo.xk.dao.CourseDao;
import cn.zhagzheguo.xk.dao.CourseSelectDao;
import cn.zhagzheguo.xk.model.Course;
import cn.zhagzheguo.xk.model.CourseSelect;
import cn.zhagzheguo.xk.status.CommonStatus;
import cn.zhagzheguo.xk.status.CourseSelectStatus;
import cn.zhagzheguo.xk.util.DateFormatUtil;

@Controller
@RequestMapping("/cs")
public class CourseSelectController {

	@Autowired
	private CourseSelectDao csDao;

	@Autowired
	private CourseDao courseDao;

	@RequestMapping("/select")
	@ResponseBody
	@Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = { Exception.class }, timeout = 20, value = "txManager")
	public String select(@RequestParam("course_id") int course_id, @RequestParam("stu_id") int stu_id) {

		CourseSelect courseSelect = csDao.getByStuIdAndCourseId(stu_id, course_id);

		/* 若有此选课且ispassed！=0，则说明该课程已选择 */
		if (courseSelect != null) {
			if (courseSelect.getIsPassed() == 0) {
				return CourseSelectStatus.ISSELECTED;
			}
		}
		Course thisCourse = courseDao.getCourse(course_id);
		/* 找不到课程 */
		if (thisCourse == null) {
			return CourseSelectStatus.WRONGID;
		}
		/* 课程已满 */
		if (thisCourse.getEnrolledNumber() == thisCourse.getCapacity()) {
			return CourseSelectStatus.ISFULL;
		}
		/* 有与已选课程时间冲突的课 */
		List<CourseSelect> list = csDao.getDetailByStuId(stu_id);
		System.out.println(list.toString());
		float currentCredit = 0;
		for (CourseSelect cs : list) {
			currentCredit += Float.parseFloat(cs.getCourse().getCredit());
			if (cs.getCourse().getClasses().equals(thisCourse.getClasses())
					&& cs.getCourse().getWeeks().equals(thisCourse.getWeeks())) {
				return CourseSelectStatus.CONFLICT;
			}
		}
		/* 可选学分已满 */
		if ((currentCredit + Float.parseFloat(thisCourse.getCredit())) > Constant.MAX_CREDIT) {
			return CourseSelectStatus.FULLCREDIT;
		}

		CourseSelect cs = new CourseSelect();
		cs.setStu_id(stu_id);
		cs.setCourse_id(course_id);
		cs.setDate(DateFormatUtil.format(new Date()));
		cs.setIsPassed(0);
		cs.setScore(0);
		int result = csDao.insert(cs);
		/* 成功 */
		if (result == CommonStatus.SUCCESS) {
			courseDao.updateEnrolledNumber(course_id, thisCourse.getEnrolledNumber() + 1);
			return CourseSelectStatus.SUCCESS;
		}
		return CourseSelectStatus.FAIL;
	}

	@RequestMapping("/remove")
	@ResponseBody
	public String delete(@RequestParam("course_id") int course_id, @RequestParam("stu_id") int stu_id) {
		System.out.println("/cs/remove course_id=" + course_id + ",stu_id=" + stu_id);
		int result = csDao.delete(stu_id, course_id);
		if (result == CommonStatus.SUCCESS) {
			return "success";
		}
		return "fail";
	}

	@RequestMapping(value = "/selected", method = RequestMethod.POST)
	@ResponseBody
	public List<Course> getSelectedBriefList(@RequestParam("id") int id) {
		List<Course> list = csDao.getBriefByStuId(id);
		System.out.println("selected:id=" + id + ",course size=" + list.size());
		return list;
	}

	/****************/
	public void setCsDao(CourseSelectDao csDao) {
		this.csDao = csDao;
	}

	public void setCourseDao(CourseDao courseDao) {
		this.courseDao = courseDao;
	}

}
